package com.example.myapp;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapp.R.menu;
import com.xuyang.dialog.CallWaiterDialog;
import com.xuyang.dialog.ConfirmSubmitOrderDialog;
import com.xuyang.flags.Menus;
import com.xuyang.util.FoodMenu;
import com.xuyang.util.SqliteDatabaseManager;


public class MainActivity extends Activity {
	private ExpandableListView mListView;
	private ImageView mImageCall;
	private ImageView mImageSubmit;
	private TextView mTxtTotal;
	public MyExpandableListViewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到所有控件
        findViewsById();
        //每次进入主页面，从本地数据库读取菜单
        Menus.clearMenu();
        SqliteDatabaseManager.getInstance(getApplicationContext()).getMenuData();
        //设置点击事件监听
        setClickListener();
        //为可拓展列表绑定数据
        adapter=new MyExpandableListViewAdapter(this,mTxtTotal);
        mListView.setAdapter(adapter);
    }
    private void findViewsById(){
    	mListView=(ExpandableListView) findViewById(R.id.listview_menu);
    	mImageCall=(ImageView) findViewById(R.id.image_download);
    	mImageSubmit=(ImageView) findViewById(R.id.image_submit);
    	mTxtTotal=(TextView) findViewById(R.id.txt_total);
    }
    private void setClickListener(){
    	mImageCall.setOnClickListener(new MyOnClickListener());
    	mImageSubmit.setOnClickListener(new MyOnClickListener());
    }
    class MyOnClickListener implements OnClickListener{
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.image_submit:
				new ConfirmSubmitOrderDialog(MainActivity.this,adapter);
				break;
			case R.id.image_download:
				new CallWaiterDialog(MainActivity.this);
				break;
			default:
				break;
			}
		}
    }
	@Override
	protected void onDestroy() {
		//在页面退出时将所有数据全部清空
		Menus.DatabaseVersion=0;
		Menus.menus.clear();
		Menus.menuTitles.clear();
		Menus.order.clear();
		Menus.SeatNumber=0;
		super.onDestroy();
	}
    
}
